########################################################################
#  Pyfm - personal finance data-base
#  Copyright (C) 2007,8 Ehud Ben-Reuven
#  udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################################
"""export command."""
from model.modellib import *
from model.tagsyntax import DTASOF
from model.trnlib import gettransaction_filter, gettransactions
from obj.flat import stagvalue
import lib.dirs
from export_ofx import do_export_ofx
from export_flat import export_flat


def do_export(self,line):
    """export EXT=<format> [ACCTID=Fm] [DTASOF=NOW] [DTEND=NOW] [DTSTART=1-Jan-...] <flat-line>
Export Holdings,Transactions,Statement,History to format. Possible options:
 * `ofx` - OFX v1 format to be used by MS Moeny.
 * `qfx` - OFX v1 file to be used by Quicken. In this case you must also specifiy a value for BID
 * `txt` - Flat lines

For ofx,qfx:
The flat line contain parameters that control what is being exported:
 * DTSTART, DTEND - Information regarding transactions, statements and history are limited to be between DTSTART (oldest date by default) and DTEND (NOW by default).
 * FILE - You can specify the full-path of where you want your exported file to appear.
By default, output will be written to $PYFM_DATA/outputs/<ACCTID>-<DTEND>-<DTSTART>.<EXT>
where <DTEND> and <DTSTART> will be represented by a six digit number.  
 * ACCTID - All holding, transactions and statement information are made to look as if it came from <ACCTID> in the output file.
Default: a common real ACCTID to all Holdings will be used.
 * ACCTTYPE - CHECKING or INVESTMENT (default.) This can generate different structure to be used in the output file. If CHECKING then only CHECKING holdings will be used and if INVESTMENT all other holdings will be used.
 * CURDEF - Default: Common value to all holdings will be used. All prices/values will be converted to it.
 * Holding - Select holdings to be exported. Default: all
 * BID - Bank Identifier that you pretend to have generated the file. This tag is used and must be specified when `EXT=qfx` (exporting to Quicken). Legal values should be taken from the right most column of the file located at: `C:\Documents and Settings\All Users\Application Data\Intuit\Quicken\Inet\Common\Localweb\Banklist\fidir.zip` You can pick up any bank you like but it has to support web connection and, depending on your assignment to ACCTTYPE, the bank should appear in the file to support investment or checking.
"""
    s=line.pop('EXT',['ofx'])[0]
        
    dtstart,dtend=popdtstartend(line)
    acctid=line.pop('ACCTID',['Fm'])[0]
    brokerid=line.pop('BROKERID',['pyfm'])[0]
    if s=='txt': acctid='Base'

    fname=line.pop('FILE',[lib.dirs.outputs("%s-%s-%s.%s"%(acctid,dtend.str6(),dtstart.str6(),s))])[0]
    pinfo('Exporting to '+fname)
        
    fout = open(fname,'w')
    if s in ['ofx','qfx']:
        do_export_ofx(fout,s,dtstart,dtend,line,brokerid,acctid)
    elif s=='txt':
        export_flat(fout,dtstart,dtend,line)
    else:
        perr('Unknwon file extension '+s)
    fout.close()
